package io.agora.vlive.ui.live;

import android.Manifest;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.PackageManager;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.os.Handler;
import android.view.SurfaceView;
import android.view.TextureView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;

import com.elvishew.xlog.XLog;

import java.util.List;
import java.util.Map;

import io.agora.capture.video.camera.CameraManager;
import io.agora.framework.PreprocessorFaceUnity;
import io.agora.framework.RtcVideoConsumer;
import io.agora.capture.video.camera.VideoModule;
import io.agora.rtc.Constants;
import io.agora.rtc.IRtcEngineEventHandler;
import io.agora.rtc.RtcEngine;
import io.agora.rtc.video.VideoCanvas;
import io.agora.rtm.ErrorInfo;
import io.agora.rtm.ResultCallback;
import io.agora.rtm.RtmChannelAttribute;
import io.agora.rtm.RtmChannelMember;
import io.agora.vlive.Config;
import io.agora.vlive.R;
import io.agora.vlive.agora.rtc.RtcEventHandler;
import io.agora.vlive.agora.rtm.RtmMessageManager;
import io.agora.vlive.agora.rtm.RtmMessageListener;
import io.agora.vlive.agora.rtm.model.GiftRankMessage;
import io.agora.vlive.agora.rtm.model.NotificationMessage;
import io.agora.vlive.agora.rtm.model.PKMessage;
import io.agora.vlive.agora.rtm.model.SeatStateMessage;
import io.agora.vlive.ui.BaseActivity;
import io.agora.vlive.utils.Global;

/**
 * Common capabilities of a live room. Such as, camera capture，
 * , agora rtc, messaging, permission check, communication with
 * the back-end server, and so on.
 */
public abstract class LiveBaseActivity extends BaseActivity
        implements RtcEventHandler, RtmMessageListener {
    protected static final String[] PERMISSIONS = {
            Manifest.permission.CAMERA,
            Manifest.permission.RECORD_AUDIO,
            Manifest.permission.WRITE_EXTERNAL_STORAGE
    };

    private static final int PERMISSION_REQ = 1;

    // values of a live room
    protected String roomName;
    protected String roomId;
    protected boolean isOwner;
    protected String ownerId;
    protected boolean isHost;
    protected int myRtcRole;
    protected int ownerRtcUid;
    protected int tabId;

    // Current rtc channel generated by server
    // and obtained when entering the room.
    protected String rtcChannelName;

    private RtmMessageManager mMessageManager;
    private CameraManager mCameraVideoManager;
    private PreprocessorFaceUnity mFUPreprocessor;

    private NetworkReceiver mNetworkReceiver = new NetworkReceiver();

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        keepScreenOn(getWindow());
        checkPermissions();
    }

    protected void checkPermissions() {
        if (!permissionArrayGranted()) {
            ActivityCompat.requestPermissions(this, PERMISSIONS, PERMISSION_REQ);
        } else {
            performInit();
        }
    }

    private boolean permissionGranted(String permission) {
        return ContextCompat.checkSelfPermission(
                this, permission) == PackageManager.PERMISSION_GRANTED;
    }

    private boolean permissionArrayGranted() {
        boolean granted = true;
        for (String per : PERMISSIONS) {
            if (!permissionGranted(per)) {
                granted = false;
                break;
            }
        }
        return granted;
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        if (requestCode == PERMISSION_REQ) {
            if (permissionArrayGranted()) {
                performInit();
            } else {
                Toast.makeText(this, R.string.permission_not_granted, Toast.LENGTH_LONG).show();
                finish();
            }
        }
    }

    private void performInit() {
        initRoom();
        onPermissionGranted();
    }

    private void initRoom() {
        Intent intent = getIntent();
        roomName = intent.getStringExtra(Global.Constants.KEY_ROOM_NAME);
        roomId = intent.getStringExtra(Global.Constants.KEY_ROOM_ID);
        isOwner = intent.getBooleanExtra(Global.Constants.KEY_IS_ROOM_OWNER, false);
        ownerId = intent.getStringExtra(Global.Constants.KEY_ROOM_OWNER_ID);
        isHost = isOwner;
        myRtcRole = isOwner ? Constants.CLIENT_ROLE_BROADCASTER : Constants.CLIENT_ROLE_AUDIENCE;
        tabId = intent.getIntExtra(Global.Constants.TAB_KEY, 0);

        mMessageManager = RtmMessageManager.instance();
        mMessageManager.init(rtmClient());
        mMessageManager.registerMessageHandler(this);
        mMessageManager.setCallbackThread(new Handler(getMainLooper()));

        proxy().registerProxyListener(this);
        registerRtcHandler(this);

        initCameraIfNeeded();
    }

    private void initCameraIfNeeded() {
        if (mCameraVideoManager == null) {
            mCameraVideoManager = cameraVideoManager();
        }

        if (mCameraVideoManager != null) {
            mCameraVideoManager.enablePreprocessor(
                config().isBeautyEnabled());
        }

        if (mFUPreprocessor == null &&
                mCameraVideoManager != null) {
            mFUPreprocessor = (PreprocessorFaceUnity)
                mCameraVideoManager.getPreprocessor();
        }
    }

    protected abstract void onPermissionGranted();

    protected RtmMessageManager getMessageManager() {
        return mMessageManager;
    }

    protected void joinRtcChannel() {
        rtcEngine().setClientRole(myRtcRole);
        rtcEngine().setVideoSource(new RtcVideoConsumer(VideoModule.instance()));
        setVideoConfiguration();
        rtcEngine().joinChannel(config().getUserProfile().getRtcToken(),
                rtcChannelName, null, (int) config().getUserProfile().getAgoraUid());
    }

    protected SurfaceView setupRemoteVideo(int uid) {
        SurfaceView surfaceView = RtcEngine.CreateRendererView(this);
        rtcEngine().setupRemoteVideo(new VideoCanvas(
                surfaceView, VideoCanvas.RENDER_MODE_HIDDEN, uid));
        return surfaceView;
    }

    protected void remoteRemoteVideo(int uid) {
        rtcEngine().setupRemoteVideo(new VideoCanvas(null, VideoCanvas.RENDER_MODE_HIDDEN, uid));
    }

    protected void setVideoConfiguration() {
        rtcEngine().setVideoEncoderConfiguration(config().createVideoEncoderConfig(tabIdToLiveType(tabId)));
    }

    protected int tabIdToLiveType(int tabId) {
        if (tabId == Config.LIVE_TYPE_MULTI_HOST ||
            tabId == Config.LIVE_TYPE_SINGLE_HOST ||
            tabId == Config.LIVE_TYPE_PK_HOST ||
            tabId == Config.LIVE_TYPE_VIRTUAL_HOST) {
            return tabId;
        } else return 0;
    }

    @Override
    public void onResume() {
        super.onResume();

        IntentFilter filter = new IntentFilter(
                ConnectivityManager.CONNECTIVITY_ACTION);
        registerReceiver(mNetworkReceiver, filter);
    }

    @Override
    public void onPause() {
        super.onPause();
        unregisterReceiver(mNetworkReceiver);
    }

    protected void startCameraCapture() {
        initCameraIfNeeded();
        if (mCameraVideoManager != null) {
            enablePreProcess(config().isBeautyEnabled());
            mCameraVideoManager.startCapture();
        }
    }

    protected void setLocalPreview(SurfaceView surfaceView) {
        initCameraIfNeeded();
        if (mCameraVideoManager != null) {
            mCameraVideoManager.setLocalPreview(surfaceView);
        }
    }

    protected void setLocalPreview(TextureView textureView) {
        initCameraIfNeeded();
        if (mCameraVideoManager != null) {
            mCameraVideoManager.setLocalPreview(textureView);
        }
    }

    protected void switchCamera() {
        initCameraIfNeeded();
        if (mCameraVideoManager != null) {
            mCameraVideoManager.switchCamera();
        }
    }

    protected void stopCameraCapture() {
        initCameraIfNeeded();
        if (mCameraVideoManager != null) {
            mCameraVideoManager.stopCapture();
        }
    }

    protected void enablePreProcess(boolean enabled) {
        initCameraIfNeeded();
        if (mCameraVideoManager != null) {
            mCameraVideoManager.enablePreprocessor(enabled);
        }
    }

    protected void setBlurValue(float blur) {
        initCameraIfNeeded();
        if (mFUPreprocessor != null) {
            mFUPreprocessor.setBlurValue(blur);
        }
    }

    protected void setWhitenValue(float whiten) {
        initCameraIfNeeded();
        if (mFUPreprocessor != null) {
            mFUPreprocessor.setWhitenValue(whiten);
        }
    }

    protected void setCheekValue(float cheek) {
        initCameraIfNeeded();
        if (mFUPreprocessor != null) {
            mFUPreprocessor.setCheekValue(cheek);
        }
    }

    protected void setEyeValue(float eye) {
        initCameraIfNeeded();
        if (mFUPreprocessor != null) {
            mFUPreprocessor.setEyeValue(eye);
        }
    }

    protected void joinRtmChannel() {
        mMessageManager.joinChannel(rtcChannelName, new ResultCallback<Void>() {
            @Override
            public void onSuccess(Void aVoid) {
                XLog.d("on rtm join channel success " + rtcChannelName);
            }

            @Override
            public void onFailure(ErrorInfo errorInfo) {
                XLog.e("rtm join channel failed " + rtcChannelName + " msg:" + errorInfo.toString());
            }
        });
    }

    protected void leaveRtmChannel(ResultCallback<Void> callback) {
        mMessageManager.leaveChannel(callback);
    }

    private static class NetworkReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            ConnectivityManager cm = (ConnectivityManager) context.
                    getSystemService(Context.CONNECTIVITY_SERVICE);
            if (cm == null) return;

            NetworkInfo info = cm.getActiveNetworkInfo();
            if (info == null || !info.isAvailable() || !info.isConnected()) {
                Toast.makeText(context, R.string.network_unavailable, Toast.LENGTH_LONG).show();
            } else {
                int type = info.getType();
                if (ConnectivityManager.TYPE_WIFI == type) {
                    Toast.makeText(context, R.string.network_switch_to_wifi, Toast.LENGTH_LONG).show();
                } else if (ConnectivityManager.TYPE_MOBILE == type) {
                    Toast.makeText(context, R.string.network_switch_to_mobile , Toast.LENGTH_LONG).show();
                }
            }
        }
    }

    @Override
    public void onRtmConnectionStateChanged(int state, int reason) {

    }

    @Override
    public void onRtmTokenExpired() {

    }

    @Override
    public void onRtmPeersOnlineStatusChanged(Map<String, Integer> map) {

    }

    @Override
    public void onRtmMemberCountUpdated(int memberCount) {

    }

    @Override
    public void onRtmAttributesUpdated(List<RtmChannelAttribute> attributeList) {

    }

    @Override
    public void onRtmMemberJoined(RtmChannelMember rtmChannelMember) {

    }

    @Override
    public void onRtmMemberLeft(RtmChannelMember rtmChannelMember) {

    }

    @Override
    public void onRtmInvitedByOwner(String ownerId, String nickname, int index) {

    }

    @Override
    public void onRtmAppliedForSeat(String peerId, String nickname, String userId, int index) {

    }

    @Override
    public void onRtmInvitationAccepted(String peerId, String nickname, int index) {

    }

    @Override
    public void onRtmApplicationAccepted(String peerId, String nickname, int index) {

    }

    @Override
    public void onRtmInvitationRejected(String peerId, String nickname) {

    }

    @Override
    public void onRtmApplicationRejected(String peerId, String nickname) {

    }

    @Override
    public void onRtmPkReceivedFromAnotherHost(String peerId, String nickname, String pkRoomId) {

    }

    @Override
    public void onRtmPkAcceptedByTargetHost(String peerId, String nickname) {

    }

    @Override
    public void onRtmPkRejectedByTargetHost(String peerId, String nickname) {

    }

    @Override
    public void onRtmChannelMessageReceived(String peerId, String nickname, String content) {

    }

    @Override
    public void onRtmRoomGiftRankChanged(int total, List<GiftRankMessage.GiftRankItem> list) {

    }

    @Override
    public void onRtmOwnerStateChanged(String userId, String userName, int uid, int enableAudio, int enableVideo) {

    }

    @Override
    public void onRtmSeatStateChanged(List<SeatStateMessage.SeatStateMessageDataItem> data) {

    }

    @Override
    public void onRtmPkStateChanged(PKMessage.PKMessageData messageData) {

    }

    @Override
    public void onRtmGiftMessage(String fromUserId, String fromUserName, String toUserId, String toUserName, int giftId) {

    }

    @Override
    public void onRtmChannelNotification(int total, List<NotificationMessage.NotificationItem> list) {

    }

    @Override
    public void onRtmLeaveMessage() {

    }

    @Override
    public void onRtcJoinChannelSuccess(String channel, int uid, int elapsed) {

    }

    @Override
    public void onRtcRemoteVideoStateChanged(int uid, int state, int reason, int elapsed) {

    }

    @Override
    public void onRtcStats(IRtcEngineEventHandler.RtcStats stats) {

    }

    @Override
    public void onRtcChannelMediaRelayStateChanged(int state, int code) {

    }

    @Override
    public void onRtcChannelMediaRelayEvent(int code) {

    }

    @Override
    public void onRtcAudioVolumeIndication(IRtcEngineEventHandler.AudioVolumeInfo[] speakers, int totalVolume) {

    }

    @Override
    public void onRtcAudioRouteChanged(int routing) {

    }

    @Override
    public void finish() {
        super.finish();
        removeRtcHandler(this);
        rtcEngine().leaveChannel();

        if (mMessageManager != null) {
            mMessageManager.removeMessageHandler(this);
            mMessageManager.leaveChannel(new ResultCallback<Void>() {
                @Override
                public void onSuccess(Void aVoid) {

                }

                @Override
                public void onFailure(ErrorInfo errorInfo) {

                }
            });
        }
    }
}
